package com.yungu.swift.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.core.io.ClassPathResource;

import java.util.Map;

/***
 * @Description
 * @author xiuyin.cui
 * @date 2019/03/25 10:06
 *
 * @return
 */
public class YamlUtils {

    private static final Logger logger = LoggerFactory.getLogger(YamlUtils.class);

    private YamlUtils() {

    }

    /**
     * @author xiuyin.cui
     * @date 2019/03/26 15:36
     * @see #load(String)
     */
    @Deprecated
    public static Map loadClassPathProperties(String fileName) {
        YamlMapFactoryBean yamlMapFactoryBean = new YamlMapFactoryBean();
        yamlMapFactoryBean.setResources(new ClassPathResource(fileName));
        logger.info("load properties file from : " + fileName);
        return yamlMapFactoryBean.getObject();
    }


    public static Map<String, Object> load(String fileName) {
        YamlMapFactoryBean yamlMapFactoryBean = new YamlMapFactoryBean();
        yamlMapFactoryBean.setResources(new ClassPathResource(fileName));
        logger.info("load properties file from : " + fileName);
        return yamlMapFactoryBean.getObject();
    }


    public static String getFromDefault(String key) {
        Map<String, Object> map = load("application.yml");
        return String.valueOf(map.get(key));
    }
}
